<!--学生查看活动详情和报名活动-->
<link rel="stylesheet" href="./css/lookactivity.css">
<link rel="stylesheet" type="text/css" href="http://at.alicdn.com/t/font_1582605_ty6g2b8p8es.css">
<link rel="stylesheet" type="text/css" href="http://at.alicdn.com/t/font_1582605_bqc2w83xwls.css">

<div class="modal fade" id="bm" tabindex="-1" role="dialog" aria-labelledby="MymodalLabel">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<h3 style="text-align: center;">报名成功</h3>
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
			</div>
			<div class="modal-body">
				<p>报名成功，请等待结果&hellip;</p>
			</div>
			<div class="modal-footer">
				<button type="button" id="modal_bm" class="btn btn-primary" data-dismiss="modal">关闭</button>
			</div>
		</div>
	</div>
</div>

<div class="modal fade" id="quxiao" tabindex="-1" role="dialog" aria-labelledby="MymodalLabel">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<h3 style="text-align: center;">取消报名成功</h3>
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
			</div>
			<div class="modal-body">
				<p>您已成功取消报名&hellip;</p>
			</div>
			<div class="modal-footer">
				<button type="button" id="modal_quxiao" class="btn btn-primary" data-dismiss="modal">关闭</button>
			</div>
		</div>
	</div>
</div>

<div class="modal fade" id="bm1" tabindex="-1" role="dialog" aria-labelledby="MymodalLabel">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<h3 style="text-align: center;">多人报名</h3>
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
			</div>
			<div class="modal-body" id="body">
				<form class="form-horizontal">
					<div class="form-group">
						<label class="sr-only" for="sno1">学号</label>
						<input type="text" class="form-control" id="sno1" placeholder="多人报名填写学号时请以英文逗号分隔开" style="width:60%;margin-left:120px;">
					</div>
				</form>
			</div>
			<div class="modal-footer" id="foot">
				<button type="button" class="btn btn-primary" data-dismiss="modal" id="confrim">确认</button>
				<button type="button" class="btn btn-primary" data-dismiss="modal">关闭</button>
			</div>
		</div>
	</div>
</div>
<div class="modal fade" id="bm2" tabindex="-1" role="dialog" aria-labelledby="MymodalLabel">
	<div class="modal-dialog" role="document">
		<div class="modal-content">
			<div class="modal-header">
				<button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
			</div>
			<div class="modal-body">
				<p>
					请填写姓名
				</p>
				<div class="content">
				</div>
			</div>
			<div class="modal-footer">
				<button type="button" class="btn btn-primary" data-dismiss="modal">关闭</button>
			</div>
		</div>
	</div>
</div>

<div id="big">

</div>

<script>
	scroll(0, 0);

	var myajax = $.ajax({
		xhrFields: {
			withCredentials: true
		},
		type: "GET",
		url: getFullURL("get-one-a"),
		data: {
			aid: $.session.get('aid')
		},
		success: function(data) {
			console.log(data);
			if(data.status == 0) {
				var str = `
	          	<div id="body_div">
					<h3 class="text-left text-h3">` + data.activity.title + `</h3>
					<h5 class="text-h4">发布人：` + data.activity.teacher.name + ` &nbsp;&nbsp;报名开始时间：` + timeStamp2String(data.activity.startTime) + ` &nbsp;&nbsp;报名截止时间：` + timeStamp2String(data.activity.endTime) + `</h5>
					<div class="main">` + data.activity.context + `</div>
					<div class="text-right" id="button_div">
						<p id="shenhe"></p>
						<button type="button" class="btn btn-primary" id="baoming1" value="` + data.activity.aid + `">单人报名</button>
						<button type="button" class="btn btn-primary" id="baoming2" value="` + data.activity.aid + `">多人报名</button>
						<button type="button" class="btn btn-primary" id="cancel_" value="` + data.activity.aid + `" disabled>取消报名</button>
					</div>
				</div>
	          `;

				var div = $('<div></div>');
				div.html(str);
				$('#big').append(div);

				$.session.set("start_a", timeStamp2String(data.activity.startTime));
				$.session.set("end_a", timeStamp2String(data.activity.endTime));
			}
		},
		error: function(err) {
			alert("请求异常，请重新登录！", function() {
				$(location).attr('href', './login.html');
			})
		}
	})

	$.when(myajax).done(function() {
		$.ajax({
			xhrFields: {
				withCredentials: true
			},
			type: "GET",
			url: getFullURL("user/status"),
			data: {
				aid: $.session.get('aid')
			},
			success: function(data) {
				console.log(data)
				if(data.status == 0) {
					if(data.findByAid != null) {
						$('#baoming1').attr("disabled", true);
						$('#baoming2').attr("disabled", true);
						$('#cancel_').removeAttr("disabled");
						if(data.findByAid.status == 0) {
							$('#shenhe').html("审核中");
						} else if(data.findByAid.status == 1) {
							$('#shenhe').html("报名成功");
						} else if(data.findByAid.status == -1) {
							$('#shenhe').html("报名失败");
						}
					} else {
						$('#baoming1').removeAttr("disabled");
						$('#baoming2').removeAttr("disabled");
						$('#cancel_').attr("disabled", true);
						$('#shenhe').html("未报名");
					}
				}
			},
			error: function(err) {
				alert("请求异常，请重新登录！", function() {
					$(location).attr('href', './login.html');
				})
			}
		})
	})

	$.when(myajax).done(function() {
		$('#modal_bm').click(function() {
			setTimeout(function() {
				$('.center').html('');
				$('.center').load('./lookactivity.html');
			}, 300);
		})

		$('#confrim').click(function() {
			setTimeout(function() {
				var array = new Array();
				var value = $('#sno1').val();
				value = value.replace("，", ",");
				array = value.split(','); //分割字符串

				$.ajax({
					xhrFields: {
						withCredentials: true
					},
					type: "GET",
					url: getFullURL("user/users-activity"),
					data: {
						aid: $.session.get('aid'),
						array: array.toString()
					},
					success: function(data) {
						$('#bm').modal({
							keyboard: true,
							show: true
						})
					},
					error: function(err) {
						alert("请求异常，请重新登录！", function() {
							$(location).attr('href', './login.html');
						})
					}
				})
			}, 300);
		})

		$('#modal_quxiao').click(function() {
			setTimeout(function() {
				$('.center').html('');
				$('.center').load('./lookactivity.html');
			}, 300);
		})
		//单人报名
		$('#baoming1').click(function() {
			var start_date = new Date($.session.get("start_a")).getTime();
			var end_date = new Date($.session.get("end_a")).getTime();
			var myDate = new Date().getTime();

			if(myDate < start_date || myDate > end_date) {
				alert("未在活动时间！");
				return;
			}

			var array = new Array()

			$.ajax({
				xhrFields: {
					withCredentials: true
				},
				type: "GET",
				url: getFullURL("user/users-activity"),
				data: {
					aid: $.session.get('aid'),
					array: array.toString()
				},
				success: function(data) {
					$('#bm').modal({
						keyboard: true,
						show: true
					})
				},
				error: function(err) {
					alert("请求异常，请重新登录！", function() {
						$(location).attr('href', './login.html');
					})
				}
			})
		})
		//多人报名
		$('#baoming2').click(function() {
			var start_date = new Date($.session.get("start_a")).getTime();
			var end_date = new Date($.session.get("end_a")).getTime();
			var myDate = new Date().getTime();

			if(myDate < start_date || myDate > end_date) {
				alert("未在活动时间！");
				return;
			}

			$('#bm1').modal({
				keyboard: true,
				show: true
			})
		})

		$('#cancel_').click(function() {
			var start_date = new Date($.session.get("start_a")).getTime();
			var end_date = new Date($.session.get("end_a")).getTime();
			var myDate = new Date().getTime();

			if(myDate < start_date || myDate > end_date) {
				alert("未在活动时间！");
				return;
			}

			$.ajax({
				xhrFields: {
					withCredentials: true
				},
				type: "GET",
				url: getFullURL("user/cancel-activity"),
				data: {
					aid: $.session.get('aid')
				},
				success: function(data) {
					if(data.status == 0) {
						$('#quxiao').modal({
							keyboard: true,
							show: true
						})
					}
				},
				error: function(err) {
					alert("请求异常，请重新登录！", function() {
						$(location).attr('href', './login.html');
					})
				}
			})
		})
	})

	function timeStamp2String(time) {
		var datetime = new Date();
		datetime.setTime(time);
		var year = datetime.getFullYear();
		var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
		var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
		var hour = datetime.getHours() < 10 ? "0" + datetime.getHours() : datetime.getHours();
		var minute = datetime.getMinutes() < 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
		var second = datetime.getSeconds() < 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
		return year + "-" + month + "-" + date + " " + hour + ":" + minute + ":" + second;
	}
</script>